﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Net.Mail;
using System.Net.Configuration;

public partial class EMailSender : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack) {
        // TODO: зашифрувати секцію "system.net/mailSettings/smtp" конфігураційного файлу web.config, оскільки в ній присутній пароль
        SmtpSection smtp = (SmtpSection)WebConfigurationManager.GetSection("system.net/mailSettings/smtp");
        if (smtp != null) {
          txtFrom.Text = smtp.Network.UserName;
        } else {
          lblResult.Text = "Error: Can't define address From";
          btnSend.Enabled = false;
        }

      }
    }

    protected void btnSend_Click(object sender, EventArgs e) {
      MailMessage email = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.Text);
      SmtpClient server = new SmtpClient();
      try {
        server.Send(email);
        lblResult.Text = "Email sent successfully";
      } catch (Exception ex) {
        lblResult.Text = "Error: " + ex.Message;
      }
    }
}